#!/usr/bin/python

from PyQt4 import QtCore, QtGui, QtWebKit
from optparse import OptionParser

class Thumbnailer(QtCore.QObject):
    def __init__(self, url, filename, viewport = QtCore.QSize(800, 600), scale = .25):
        QtCore.QObject.__init__(self)
        self.viewport = viewport
        self.scale = scale
        self.filename = filename
        self.page = QtWebKit.QWebPage()
        self.connect(self.page, QtCore.SIGNAL("loadFinished(bool)"), self.render)
        self.page.mainFrame().load(QtCore.QUrl(url))
    
    def render(self, ok):
        page = self.page
        page.setViewportSize(self.viewport)
        image = QtGui.QImage(page.viewportSize(), QtGui.QImage.Format_ARGB32)
        painter = QtGui.QPainter(image)
        page.mainFrame().render(painter)
        painter.end()
        thumbnail = QtGui.QImage(image.scaled(self.viewport * self.scale, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
        thumbnail.save(self.filename);
        QtCore.QCoreApplication.quit()

if __name__ == "__main__":
    parser = OptionParser(usage = "usage: %prog [options] url")
    parser.add_option("-o", "--outfile", dest = "filename", help = "write image to filename")
    parser.add_option("-v", "--viewport", dest = "viewport", help = "size of viewport", metavar = "WxH", default = "800x600")
    parser.add_option("-s", "--scale", dest = "scale", help = "viewport is scaled by factor scale for final image", default = ".25")
    (options, args) = parser.parse_args()
    app = QtGui.QApplication([])
    tmp = map(int, options.viewport.split("x"))
    viewport = QtCore.QSize(*tmp)
    thumb = Thumbnailer(args[0], viewport = viewport, scale = float(options.scale), filename = options.filename)
    app.exec_()
